<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>09-AntD框架的upload组件上传图片时遇到的一些坑</title>
  
    
      <meta 
        property="og:title" 
        content="09-AntD框架的upload组件上传图片时遇到的一些坑">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2023-03-09">
      <meta 
        property="og:article:modified_time" 
        content="2023-02-04">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-AntD-%E7%9A%84-upload-%E7%BB%84%E4%BB%B6%E5%81%9A%E5%9B%BE%E7%89%87%E7%9A%84%E4%B8%8A%E4%BC%A0"><span class="toc-text">使用 AntD 的 upload 组件做图片的上传</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8A%E4%BC%A0%E5%90%8E%EF%BC%8C%E7%82%B9%E5%87%BB%E5%9B%BE%E7%89%87%E9%A2%84%E8%A7%88%EF%BC%8C%E6%B5%8F%E8%A7%88%E5%99%A8%E5%8D%A1%E6%AD%BB%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-text">上传后，点击图片预览，浏览器卡死的问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%B0%E9%9C%80%E6%B1%82%EF%BC%9A%E7%BC%96%E8%BE%91%E7%8E%B0%E6%9C%89%E9%A1%B5%E9%9D%A2"><span class="toc-text">新需求：编辑现有页面</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E9%97%AE%E9%A2%98"><span class="toc-text">其他问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%80%E5%90%8E%E4%B8%80%E6%AE%B5"><span class="toc-text">最后一段</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      09-AntD框架的upload组件上传图片时遇到的一些坑
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2023-03-09T03:39:04.045Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2023-03-09</span>
    </time>
    
    
      <span class="dot"></span>
      <span>3.7k 字</span>
    
  </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>本次做后台管理系统，采用的是 AntD 框架。涉及到图片的上传，用的是AntD的 <a target="_blank" rel="noopener" href="https://ant.design/components/upload-cn/">upload</a> 组件。</p>
<p>前端做文件上传这个功能，是很有技术难度的。既然框架给我们提供好了，那就直接用呗。结果用的时候，发现 upload 组件的很多bug。下面来列举几个。</p>
<p>备注：本文写于2019-03-02，使用的 antd 版本是 3.13.6。</p>
<h2 id="使用-AntD-的-upload-组件做图片的上传"><a href="#使用-AntD-的-upload-组件做图片的上传" class="headerlink" title="使用 AntD 的 upload 组件做图片的上传"></a>使用 AntD 的 upload 组件做图片的上传</h2><p>因为需要上传多张图片，所以采用的是照片墙的形式。上传成功后的界面如下：</p>
<p>（1）上传中：</p>
<p><img src="http://img.smyhvae.com/20190302_1335.png"></p>
<p>（2）上传成功：</p>
<p><img src="http://img.smyhvae.com/20190302_1336.png"></p>
<p>（3）图片预览：</p>
<p><img src="http://img.smyhvae.com/20190302_1331.png"></p>
<p>按照官方提供的实例，特此整理出项目开发中的完整写法，亲测有效。代码如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* eslint-disable */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">Upload</span>, <span class="title class_">Icon</span>, <span class="title class_">Modal</span>, <span class="title class_">Form</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;antd&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">FormItem</span> = <span class="title class_">Form</span>.<span class="property">Item</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">PicturesWall</span> <span class="keyword">extends</span> <span class="title class_ inherited__">PureComponent</span> &#123;</span><br><span class="line">  state = &#123;</span><br><span class="line">    <span class="attr">previewVisible</span>: <span class="literal">false</span>,</span><br><span class="line">    <span class="attr">previewImage</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">    <span class="attr">imgList</span>: [],</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  handleChange = <span class="function">(<span class="params">&#123; file, fileList &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(file)); <span class="comment">// file 是当前正在上传的 单个 img</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(fileList)); <span class="comment">// fileList 是已上传的全部 img 列表</span></span><br><span class="line"></span><br><span class="line">    <span class="variable language_">this</span>.<span class="title function_">setState</span>(&#123;</span><br><span class="line">      <span class="attr">imgList</span>: fileList,</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  handleCancel = <span class="function">() =&gt;</span> <span class="variable language_">this</span>.<span class="title function_">setState</span>(&#123; <span class="attr">previewVisible</span>: <span class="literal">false</span> &#125;);</span><br><span class="line"></span><br><span class="line">  handlePreview = <span class="function"><span class="params">file</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="title function_">setState</span>(&#123;</span><br><span class="line">      <span class="attr">previewImage</span>: file.<span class="property">url</span> || file.<span class="property">thumbUrl</span>,</span><br><span class="line">      <span class="attr">previewVisible</span>: <span class="literal">true</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 参考链接：https://www.jianshu.com/p/f356f050b3c9</span></span><br><span class="line">  handleBeforeUpload = <span class="function"><span class="params">file</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//限制图片 格式、size、分辨率</span></span><br><span class="line">    <span class="keyword">const</span> isJPG = file.<span class="property">type</span> === <span class="string">&#x27;image/jpeg&#x27;</span>;</span><br><span class="line">    <span class="keyword">const</span> isJPEG = file.<span class="property">type</span> === <span class="string">&#x27;image/jpeg&#x27;</span>;</span><br><span class="line">    <span class="keyword">const</span> isGIF = file.<span class="property">type</span> === <span class="string">&#x27;image/gif&#x27;</span>;</span><br><span class="line">    <span class="keyword">const</span> isPNG = file.<span class="property">type</span> === <span class="string">&#x27;image/png&#x27;</span>;</span><br><span class="line">    <span class="keyword">if</span> (!(isJPG || isJPEG || isGIF || isPNG)) &#123;</span><br><span class="line">      <span class="title class_">Modal</span>.<span class="title function_">error</span>(&#123;</span><br><span class="line">        <span class="attr">title</span>: <span class="string">&#x27;只能上传JPG 、JPEG 、GIF、 PNG格式的图片~&#x27;</span>,</span><br><span class="line">      &#125;);</span><br><span class="line">      <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> isLt2M = file.<span class="property">size</span> / <span class="number">1024</span> / <span class="number">1024</span> &lt; <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">if</span> (!isLt2M) &#123;</span><br><span class="line">      <span class="title class_">Modal</span>.<span class="title function_">error</span>(&#123;</span><br><span class="line">        <span class="attr">title</span>: <span class="string">&#x27;超过2M限制，不允许上传~&#x27;</span>,</span><br><span class="line">      &#125;);</span><br><span class="line">      <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> (isJPG || isJPEG || isGIF || isPNG) &amp;&amp; isLt2M &amp;&amp; <span class="variable language_">this</span>.<span class="title function_">checkImageWH</span>(file);</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//返回一个 promise：检测通过则返回resolve；失败则返回reject，并阻止图片上传</span></span><br><span class="line">  <span class="title function_">checkImageWH</span>(<span class="params">file</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> self = <span class="variable language_">this</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="keyword">function</span>(<span class="params">resolve, reject</span>) &#123;</span><br><span class="line">      <span class="keyword">let</span> filereader = <span class="keyword">new</span> <span class="title class_">FileReader</span>();</span><br><span class="line">      filereader.<span class="property">onload</span> = <span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> src = e.<span class="property">target</span>.<span class="property">result</span>;</span><br><span class="line">        <span class="keyword">const</span> image = <span class="keyword">new</span> <span class="title class_">Image</span>();</span><br><span class="line">        image.<span class="property">onload</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">          <span class="comment">// 获取图片的宽高，并存放到file对象中</span></span><br><span class="line">          <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;file width :&#x27;</span> + <span class="variable language_">this</span>.<span class="property">width</span>);</span><br><span class="line">          <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;file height :&#x27;</span> + <span class="variable language_">this</span>.<span class="property">height</span>);</span><br><span class="line">          file.<span class="property">width</span> = <span class="variable language_">this</span>.<span class="property">width</span>;</span><br><span class="line">          file.<span class="property">height</span> = <span class="variable language_">this</span>.<span class="property">height</span>;</span><br><span class="line">          <span class="title function_">resolve</span>();</span><br><span class="line">        &#125;;</span><br><span class="line">        image.<span class="property">onerror</span> = reject;</span><br><span class="line">        image.<span class="property">src</span> = src;</span><br><span class="line">      &#125;;</span><br><span class="line">      filereader.<span class="title function_">readAsDataURL</span>(file);</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  handleSubmit = <span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; dispatch, form &#125; = <span class="variable language_">this</span>.<span class="property">props</span>;</span><br><span class="line">    e.<span class="title function_">preventDefault</span>();</span><br><span class="line">    form.<span class="title function_">validateFieldsAndScroll</span>(<span class="function">(<span class="params">err, values</span>) =&gt;</span> &#123;<span class="comment">// values 是form表单里的参数</span></span><br><span class="line">      <span class="comment">// 点击按钮后，将表单提交给后台</span></span><br><span class="line">      <span class="title function_">dispatch</span>(&#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="string">&#x27;mymodel/submitFormData&#x27;</span>,</span><br><span class="line">        <span class="attr">payload</span>: values,</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="title function_">render</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; previewVisible, previewImage, imgList &#125; = <span class="variable language_">this</span>.<span class="property">state</span>; <span class="comment">//  从 state 中拿数据</span></span><br><span class="line">    <span class="keyword">const</span> uploadButton = (</span><br><span class="line">      <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">Icon</span> <span class="attr">type</span>=<span class="string">&quot;plus&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;ant-upload-text&quot;</span>&gt;</span>Upload<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    );</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;clearfix&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">Form</span> <span class="attr">onSubmit</span>=<span class="string">&#123;this.handleSubmit&#125;</span> <span class="attr">hideRequiredMark</span> <span class="attr">style</span>=<span class="string">&#123;&#123;</span> <span class="attr">marginTop:</span> <span class="attr">8</span> &#125;&#125;&gt;</span></span></span><br><span class="line"><span class="language-xml">          <span class="tag">&lt;<span class="name">FormItem</span> <span class="attr">label</span>=<span class="string">&quot;图片图片&quot;</span> &#123;<span class="attr">...formItemLayout</span>&#125;&gt;</span></span></span><br><span class="line"><span class="language-xml">            &#123;getFieldDecorator(&#x27;myImg&#x27;)(</span></span><br><span class="line"><span class="language-xml">              <span class="tag">&lt;<span class="name">Upload</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                <span class="attr">action</span>=<span class="string">&quot;//jsonplaceholder.typicode.com/posts/&quot;</span> // <span class="attr">这个是图片上传的接口请求</span>，<span class="attr">实际开发中</span>，<span class="attr">要替换成你自己的业务接口</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                <span class="attr">data</span>=<span class="string">&#123;file</span> =&gt;</span> (&#123; // data里存放的是接口的请求参数</span></span><br><span class="line"><span class="language-xml">                  param1: myParam1,</span></span><br><span class="line"><span class="language-xml">                  param2: myParam2,</span></span><br><span class="line"><span class="language-xml">                  photoCotent: file, // file 是当前正在上传的图片</span></span><br><span class="line"><span class="language-xml">                  photoWidth: file.height, // 通过  handleBeforeUpload 获取 图片的宽高</span></span><br><span class="line"><span class="language-xml">                  photoHeight: file.width,</span></span><br><span class="line"><span class="language-xml">                &#125;)&#125;</span></span><br><span class="line"><span class="language-xml">                listType=&quot;picture-card&quot;</span></span><br><span class="line"><span class="language-xml">                fileList=&#123;this.state.imgList&#125;</span></span><br><span class="line"><span class="language-xml">                onPreview=&#123;this.handlePreview&#125; // 点击图片缩略图，进行预览</span></span><br><span class="line"><span class="language-xml">                beforeUpload=&#123;this.handleBeforeUpload&#125; // 上传之前，对图片的格式做校验，并获取图片的宽高</span></span><br><span class="line"><span class="language-xml">                onChange=&#123;this.handleChange&#125; // 每次上传图片时，都会触发这个方法</span></span><br><span class="line"><span class="language-xml">              &gt;</span></span><br><span class="line"><span class="language-xml">                &#123;this.state.imgList.length &gt;= 9 ? null : uploadButton&#125;</span></span><br><span class="line"><span class="language-xml">              <span class="tag">&lt;/<span class="name">Upload</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            )&#125;</span></span><br><span class="line"><span class="language-xml">          <span class="tag">&lt;/<span class="name">FormItem</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">Form</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">Modal</span> <span class="attr">visible</span>=<span class="string">&#123;previewVisible&#125;</span> <span class="attr">footer</span>=<span class="string">&#123;null&#125;</span> <span class="attr">onCancel</span>=<span class="string">&#123;this.handleCancel&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">          <span class="tag">&lt;<span class="name">img</span> <span class="attr">alt</span>=<span class="string">&quot;example&quot;</span> <span class="attr">style</span>=<span class="string">&#123;&#123;</span> <span class="attr">width:</span> &#x27;<span class="attr">100</span>%&#x27; &#125;&#125; <span class="attr">src</span>=<span class="string">&#123;previewImage&#125;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">Modal</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">PicturesWall</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="上传后，点击图片预览，浏览器卡死的问题"><a href="#上传后，点击图片预览，浏览器卡死的问题" class="headerlink" title="上传后，点击图片预览，浏览器卡死的问题"></a>上传后，点击图片预览，浏览器卡死的问题</h2><p>依据上方的代码，通过 Antd 的 upload 组件将图片上传成功后，点击图片的缩略图，理应可以在当前页面弹出  Modal，预览图片。但实际的结果是，浏览器一定会卡死。</p>
<p>定位问题发现，原因竟然是：图片上传成功后， upload 会将其转为 base64编码。base64这个字符串太大了，点击图片预览的时候，浏览器在解析一大串字符串，然后就卡死了。详细过程描述如下。</p>
<p>上方代码中，我们可以把 handleChange(file, fileList)方法中的 <code>file</code>、以及 <code>fileList</code>打印出来看看。 <code>file</code>指的是当前正在上传的 单个 img，<code>fileList</code>是已上传的全部 img 列表。 当我上传完 两张图片后， 打印结果如下：</p>
<p>file的打印的结果如下：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;uid&quot;</span><span class="punctuation">:</span> <span class="string">&quot;rc-upload-1551084269812-5&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;width&quot;</span><span class="punctuation">:</span> <span class="number">600</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;height&quot;</span><span class="punctuation">:</span> <span class="number">354</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;lastModified&quot;</span><span class="punctuation">:</span> <span class="number">1546701318000</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;lastModifiedDate&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2019-01-05T15:15:18.000Z&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span> <span class="string">&quot;e30e7b9680634b2c888c8bb513cc595d.jpg&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;size&quot;</span><span class="punctuation">:</span> <span class="number">31731</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;type&quot;</span><span class="punctuation">:</span> <span class="string">&quot;image/jpeg&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;percent&quot;</span><span class="punctuation">:</span> <span class="number">100</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;originFileObj&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">        <span class="attr">&quot;uid&quot;</span><span class="punctuation">:</span> <span class="string">&quot;rc-upload-1551084269812-5&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;width&quot;</span><span class="punctuation">:</span> <span class="number">600</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;height&quot;</span><span class="punctuation">:</span> <span class="number">354</span></span><br><span class="line">    <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;status&quot;</span><span class="punctuation">:</span> <span class="string">&quot;done&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;thumbUrl&quot;</span><span class="punctuation">:</span> <span class="string">&quot;&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;response&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">        <span class="attr">&quot;retCode&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;imgUrl&quot;</span><span class="punctuation">:</span> <span class="string">&quot;http://qianguyihao.com/opfewfwj098902kpkpkkj976fe.jpg&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;photoid&quot;</span><span class="punctuation">:</span> <span class="number">271850</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<p>fileList 的打印结果：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">[</span></span><br><span class="line">    <span class="punctuation">&#123;</span></span><br><span class="line">        <span class="attr">&quot;uid&quot;</span><span class="punctuation">:</span> <span class="string">&quot;rc-upload-1551084269812-3&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;width&quot;</span><span class="punctuation">:</span> <span class="number">1000</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;height&quot;</span><span class="punctuation">:</span> <span class="number">667</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;lastModified&quot;</span><span class="punctuation">:</span> <span class="number">1501414799000</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;lastModifiedDate&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2017-07-30T11:39:59.000Z&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span> <span class="string">&quot;29381f30e924b89914e91b33.jpg&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;size&quot;</span><span class="punctuation">:</span> <span class="number">135204</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;type&quot;</span><span class="punctuation">:</span> <span class="string">&quot;image/jpeg&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;percent&quot;</span><span class="punctuation">:</span> <span class="number">100</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;originFileObj&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">            <span class="attr">&quot;uid&quot;</span><span class="punctuation">:</span> <span class="string">&quot;rc-upload-1551084269812-3&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;width&quot;</span><span class="punctuation">:</span> <span class="number">1000</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;height&quot;</span><span class="punctuation">:</span> <span class="number">667</span></span><br><span class="line">        <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;status&quot;</span><span class="punctuation">:</span> <span class="string">&quot;done&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;thumbUrl&quot;</span><span class="punctuation">:</span> <span class="string">&quot;&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;response&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">            <span class="attr">&quot;retCode&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;msg&quot;</span><span class="punctuation">:</span> <span class="string">&quot;success&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;imgUrl&quot;</span><span class="punctuation">:</span> <span class="string">&quot;http://qianguyihao.com/hfwpjouiurewnmbhepr689.jpg&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="punctuation">&#123;</span></span><br><span class="line">        <span class="attr">&quot;uid&quot;</span><span class="punctuation">:</span> <span class="string">&quot;rc-upload-1551084269812-5&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;width&quot;</span><span class="punctuation">:</span> <span class="number">600</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;height&quot;</span><span class="punctuation">:</span> <span class="number">354</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;lastModified&quot;</span><span class="punctuation">:</span> <span class="number">1546701318000</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;lastModifiedDate&quot;</span><span class="punctuation">:</span> <span class="string">&quot;2019-01-05T15:15:18.000Z&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span> <span class="string">&quot;e30e7b9680634b2c888c8bb513cc595d.jpg&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;size&quot;</span><span class="punctuation">:</span> <span class="number">31731</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;type&quot;</span><span class="punctuation">:</span> <span class="string">&quot;image/jpeg&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;percent&quot;</span><span class="punctuation">:</span> <span class="number">100</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;originFileObj&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">            <span class="attr">&quot;uid&quot;</span><span class="punctuation">:</span> <span class="string">&quot;rc-upload-1551084269812-5&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;width&quot;</span><span class="punctuation">:</span> <span class="number">600</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;height&quot;</span><span class="punctuation">:</span> <span class="number">354</span></span><br><span class="line">        <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;status&quot;</span><span class="punctuation">:</span> <span class="string">&quot;done&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;thumbUrl&quot;</span><span class="punctuation">:</span> <span class="string">&quot;&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;response&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">            <span class="attr">&quot;retCode&quot;</span><span class="punctuation">:</span> <span class="number">0</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;imgUrl&quot;</span><span class="punctuation">:</span> <span class="string">&quot;http://qianguyihao.com/opfewfwj098902kpkpkkj976fe.jpg&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;photoid&quot;</span><span class="punctuation">:</span> <span class="number">271850</span></span><br><span class="line">        <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>

<p>上方的json数据中，需要做几点解释：</p>
<p>（1）<code>response</code> 字段里面的数据，就是请求接口后，后台返回给前端的数据，里面包含了图片的url链接。</p>
<p>（2）<code>status</code> 字段里存放的是图片上传的实时状态，包括上传中、上传完成、上传失败。</p>
<p>（3）<code>thumbUrl</code>字段里面存放的是图片的base64编码。</p>
<p>这个base64编码非常非常长。当点击图片预览的时候，其实就是加载的 thumbUrl 这个字段里的资源，难怪浏览器会卡死。</p>
<p><strong>解决办法</strong>：在 handleChange方法里，图片上传成功后，将 thumbUrl 字段里面的 base64 编码改为真实的图片url。代码实现如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">handleChange = <span class="function">(<span class="params">&#123; file, fileList &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(file)); <span class="comment">// file 是当前正在上传的 单个 img</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(fileList)); <span class="comment">// fileList 是已上传的全部 img 列表</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 【重要】将 图片的base64替换为图片的url。 这一行一定不会能少。</span></span><br><span class="line">  <span class="comment">// 图片上传成功后，fileList数组中的 thumbUrl 中保存的是图片的base64字符串，这种情况，导致的问题是：图片上传成功后，点击图片缩略图，浏览器会会卡死。而下面这行代码，可以解决该bug。</span></span><br><span class="line">  fileList.<span class="title function_">forEach</span>(<span class="function"><span class="params">imgItem</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (imgItem &amp;&amp; imgItem.<span class="property">status</span> == <span class="string">&#x27;done&#x27;</span> &amp;&amp; imgItem.<span class="property">response</span> &amp;&amp; imgItem.<span class="property">response</span>.<span class="property">imgUrl</span>) &#123;</span><br><span class="line">      imgItem.<span class="property">thumbUrl</span> = imgItem.<span class="property">response</span>.<span class="property">imgUrl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="variable language_">this</span>.<span class="title function_">setState</span>(&#123;</span><br><span class="line">    <span class="attr">imgList</span>: fileList,</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>


<h2 id="新需求：编辑现有页面"><a href="#新需求：编辑现有页面" class="headerlink" title="新需求：编辑现有页面"></a>新需求：编辑现有页面</h2><p>上面一段的代码中，我们是在新建的页面中，从零开始上传图片。</p>
<p>现在有个新的需求：如何编辑现有的页面呢？也就是说，现有的页面在初始化时，是默认有几张图片的。当我编辑这个页面时，可以对现有的图片做增删，也能增加新的图片。而且要保证：新建页面和编辑现有页面，是共用一套代码。</p>
<p>我看到upload 组件有提供 <code>defaultFileList</code> 的属性。我试了下，这个<code>defaultFileList</code> 的属性根本没法儿用。</p>
<p>那就只有手动实现了。我的model层代码，是用 redux 写的。整体的实现思路如下：（这个也是在真正在实战中用到的代码）</p>
<p>（1）PicturesWall.js：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* eslint-disable */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">Upload</span>, <span class="title class_">Icon</span>, <span class="title class_">Modal</span>, <span class="title class_">Form</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;antd&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">FormItem</span> = <span class="title class_">Form</span>.<span class="property">Item</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">PicturesWall</span> <span class="keyword">extends</span> <span class="title class_ inherited__">PureComponent</span> &#123;</span><br><span class="line">  state = &#123;</span><br><span class="line">    <span class="attr">previewVisible</span>: <span class="literal">false</span>,</span><br><span class="line">    <span class="attr">previewImage</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 页面初始化的时候，从接口拉取默认的图片数据</span></span><br><span class="line">  <span class="title function_">componentDidMount</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; dispatch &#125; = <span class="variable language_">this</span>.<span class="property">props</span>;</span><br><span class="line">    <span class="title function_">dispatch</span>(&#123;</span><br><span class="line">      <span class="attr">type</span>: <span class="string">&#x27;mymodel/getAllInfo&#x27;</span>,</span><br><span class="line">      <span class="attr">payload</span>: &#123; <span class="attr">params</span>: xxx &#125;,</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  handleChange = <span class="function">(<span class="params">&#123; file, fileList &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; dispatch &#125; = <span class="variable language_">this</span>.<span class="property">props</span>;</span><br><span class="line">    <span class="comment">// 【重要】将 图片的base64替换为图片的url。 这一行一定不会能少。</span></span><br><span class="line">    <span class="comment">// 图片上传成功后，fileList数组中的 thumbUrl 中保存的是图片的base64字符串，这种情况，导致的问题是：图片上传成功后，点击图片缩略图，浏览器会会卡死。而下面这行代码，可以解决该bug。</span></span><br><span class="line">    fileList.<span class="title function_">forEach</span>(<span class="function"><span class="params">imgItem</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (imgItem &amp;&amp; imgItem.<span class="property">status</span> == <span class="string">&#x27;done&#x27;</span> &amp;&amp; imgItem.<span class="property">response</span> &amp;&amp; imgItem.<span class="property">response</span>.<span class="property">imgUrl</span>) &#123;</span><br><span class="line">        imgItem.<span class="property">thumbUrl</span> = imgItem.<span class="property">response</span>.<span class="property">imgUrl</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="title function_">dispatch</span>(&#123;</span><br><span class="line">      <span class="attr">type</span>: <span class="string">&#x27;mymodel/setImgList&#x27;</span>,</span><br><span class="line">      <span class="attr">payload</span>: fileList,</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  handleCancel = <span class="function">() =&gt;</span> <span class="variable language_">this</span>.<span class="title function_">setState</span>(&#123; <span class="attr">previewVisible</span>: <span class="literal">false</span> &#125;);</span><br><span class="line"></span><br><span class="line">  handlePreview = <span class="function"><span class="params">file</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="title function_">setState</span>(&#123;</span><br><span class="line">      <span class="attr">previewImage</span>: file.<span class="property">url</span> || file.<span class="property">thumbUrl</span>,</span><br><span class="line">      <span class="attr">previewVisible</span>: <span class="literal">true</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 参考链接：https://www.jianshu.com/p/f356f050b3c9</span></span><br><span class="line">  handleBeforeUpload = <span class="function"><span class="params">file</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//限制图片 格式、size、分辨率</span></span><br><span class="line">    <span class="keyword">const</span> isJPG = file.<span class="property">type</span> === <span class="string">&#x27;image/jpeg&#x27;</span>;</span><br><span class="line">    <span class="keyword">const</span> isJPEG = file.<span class="property">type</span> === <span class="string">&#x27;image/jpeg&#x27;</span>;</span><br><span class="line">    <span class="keyword">const</span> isGIF = file.<span class="property">type</span> === <span class="string">&#x27;image/gif&#x27;</span>;</span><br><span class="line">    <span class="keyword">const</span> isPNG = file.<span class="property">type</span> === <span class="string">&#x27;image/png&#x27;</span>;</span><br><span class="line">    <span class="keyword">const</span> isLt2M = file.<span class="property">size</span> / <span class="number">1024</span> / <span class="number">1024</span> &lt; <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!(isJPG || isJPEG || isGIF || isPNG)) &#123;</span><br><span class="line">      <span class="title class_">Modal</span>.<span class="title function_">error</span>(&#123;</span><br><span class="line">        <span class="attr">title</span>: <span class="string">&#x27;只能上传JPG 、JPEG 、GIF、 PNG格式的图片~&#x27;</span>,</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (!isLt2M) &#123;</span><br><span class="line">      <span class="title class_">Modal</span>.<span class="title function_">error</span>(&#123;</span><br><span class="line">        <span class="attr">title</span>: <span class="string">&#x27;超过2M限制，不允许上传~&#x27;</span>,</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 参考链接：https://github.com/ant-design/ant-design/issues/8779</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (!(isJPG || isJPEG || isGIF || isPNG)) &#123;</span><br><span class="line">        <span class="title function_">reject</span>(file);</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="title function_">resolve</span>(file &amp;&amp; <span class="variable language_">this</span>.<span class="title function_">checkImageWH</span>(file));</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//返回一个 promise：检测通过则返回resolve；失败则返回reject，并阻止图片上传</span></span><br><span class="line">  <span class="title function_">checkImageWH</span>(<span class="params">file</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> self = <span class="variable language_">this</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="keyword">function</span>(<span class="params">resolve, reject</span>) &#123;</span><br><span class="line">      <span class="keyword">let</span> filereader = <span class="keyword">new</span> <span class="title class_">FileReader</span>();</span><br><span class="line">      filereader.<span class="property">onload</span> = <span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> src = e.<span class="property">target</span>.<span class="property">result</span>;</span><br><span class="line">        <span class="keyword">const</span> image = <span class="keyword">new</span> <span class="title class_">Image</span>();</span><br><span class="line">        image.<span class="property">onload</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">          <span class="comment">// 获取图片的宽高，并存放到file对象中</span></span><br><span class="line">          <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;file width :&#x27;</span> + <span class="variable language_">this</span>.<span class="property">width</span>);</span><br><span class="line">          <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;file height :&#x27;</span> + <span class="variable language_">this</span>.<span class="property">height</span>);</span><br><span class="line">          file.<span class="property">width</span> = <span class="variable language_">this</span>.<span class="property">width</span>;</span><br><span class="line">          file.<span class="property">height</span> = <span class="variable language_">this</span>.<span class="property">height</span>;</span><br><span class="line">          <span class="title function_">resolve</span>();</span><br><span class="line">        &#125;;</span><br><span class="line">        image.<span class="property">onerror</span> = reject;</span><br><span class="line">        image.<span class="property">src</span> = src;</span><br><span class="line">      &#125;;</span><br><span class="line">      filereader.<span class="title function_">readAsDataURL</span>(file);</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  handleSubmit = <span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; dispatch, form &#125; = <span class="variable language_">this</span>.<span class="property">props</span>;</span><br><span class="line">    e.<span class="title function_">preventDefault</span>();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> &#123;</span><br><span class="line">      <span class="attr">mymodel</span>: &#123; imgList &#125;, <span class="comment">// 从props中拿默认的图片数据</span></span><br><span class="line">    &#125; = <span class="variable language_">this</span>.<span class="property">props</span>;</span><br><span class="line"></span><br><span class="line">    form.<span class="title function_">validateFieldsAndScroll</span>(<span class="function">(<span class="params">err, values</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="comment">// values 是form表单里的参数</span></span><br><span class="line">      <span class="comment">// 点击按钮后，将表单提交给后台</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">      <span class="comment">// start 问题描述：当编辑现有页面时，如果针对已经存在的默认图片不做修改，则不会触发 upload 的 onChange方法。此时提交表单，表单里的 myImg 字段是空的。</span></span><br><span class="line">      <span class="comment">// 解决办法：如果发现存在默认图片，则追加到表单中</span></span><br><span class="line">      <span class="keyword">if</span> (!values.<span class="property">myImg</span>) &#123;</span><br><span class="line"></span><br><span class="line">        values.<span class="property">myImg</span> = &#123; <span class="attr">fileList</span>: [] &#125;;</span><br><span class="line"></span><br><span class="line">        values.<span class="property">myImg</span>.<span class="property">fileList</span> = imgList;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// end</span></span><br><span class="line"></span><br><span class="line">      <span class="title function_">dispatch</span>(&#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="string">&#x27;mymodel/submitFormData&#x27;</span>,</span><br><span class="line">        <span class="attr">payload</span>: values,</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="title function_">render</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; previewVisible, previewImage &#125; = <span class="variable language_">this</span>.<span class="property">state</span>; <span class="comment">//  从 state 中拿数据</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> &#123;</span><br><span class="line">      <span class="attr">mymodel</span>: &#123; imgList &#125;, <span class="comment">// 从props中拿到的图片数据</span></span><br><span class="line">    &#125; = <span class="variable language_">this</span>.<span class="property">props</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> uploadButton = (</span><br><span class="line">      <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">Icon</span> <span class="attr">type</span>=<span class="string">&quot;plus&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;ant-upload-text&quot;</span>&gt;</span>Upload<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    );</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      <span class="language-xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;clearfix&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">Form</span> <span class="attr">onSubmit</span>=<span class="string">&#123;this.handleSubmit&#125;</span> <span class="attr">hideRequiredMark</span> <span class="attr">style</span>=<span class="string">&#123;&#123;</span> <span class="attr">marginTop:</span> <span class="attr">8</span> &#125;&#125;&gt;</span></span></span><br><span class="line"><span class="language-xml">          <span class="tag">&lt;<span class="name">FormItem</span> <span class="attr">label</span>=<span class="string">&quot;图片上传&quot;</span> &#123;<span class="attr">...formItemLayout</span>&#125;&gt;</span></span></span><br><span class="line"><span class="language-xml">            &#123;getFieldDecorator(&#x27;myImg&#x27;)(</span></span><br><span class="line"><span class="language-xml">              <span class="tag">&lt;<span class="name">Upload</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                <span class="attr">action</span>=<span class="string">&quot;//jsonplaceholder.typicode.com/posts/&quot;</span> // <span class="attr">这个是图片上传的接口请求</span>，<span class="attr">实际开发中</span>，<span class="attr">要替换成你自己的业务接口</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                <span class="attr">data</span>=<span class="string">&#123;file</span> =&gt;</span> (&#123;</span></span><br><span class="line"><span class="language-xml">                  // data里存放的是接口的请求参数</span></span><br><span class="line"><span class="language-xml">                  param1: myParam1,</span></span><br><span class="line"><span class="language-xml">                  param2: myParam2,</span></span><br><span class="line"><span class="language-xml">                  photoCotent: file, // file 是当前正在上传的图片</span></span><br><span class="line"><span class="language-xml">                  photoWidth: file.height, // 通过  handleBeforeUpload 获取 图片的宽高</span></span><br><span class="line"><span class="language-xml">                  photoHeight: file.width,</span></span><br><span class="line"><span class="language-xml">                &#125;)&#125;</span></span><br><span class="line"><span class="language-xml">                listType=&quot;picture-card&quot;</span></span><br><span class="line"><span class="language-xml">                fileList=&#123;imgList&#125;  // 改为从 props 里拿图片数据，而不是从 state</span></span><br><span class="line"><span class="language-xml">                onPreview=&#123;this.handlePreview&#125; // 点击图片缩略图，进行预览</span></span><br><span class="line"><span class="language-xml">                beforeUpload=&#123;this.handleBeforeUpload&#125; // 上传之前，对图片的格式做校验，并获取图片的宽高</span></span><br><span class="line"><span class="language-xml">                onChange=&#123;this.handleChange&#125; // 每次上传图片时，都会触发这个方法</span></span><br><span class="line"><span class="language-xml">              &gt;</span></span><br><span class="line"><span class="language-xml">                &#123;this.state.imgList.length &gt;= 9 ? null : uploadButton&#125;</span></span><br><span class="line"><span class="language-xml">              <span class="tag">&lt;/<span class="name">Upload</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">            )&#125;</span></span><br><span class="line"><span class="language-xml">          <span class="tag">&lt;/<span class="name">FormItem</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">Form</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;<span class="name">Modal</span> <span class="attr">visible</span>=<span class="string">&#123;previewVisible&#125;</span> <span class="attr">footer</span>=<span class="string">&#123;null&#125;</span> <span class="attr">onCancel</span>=<span class="string">&#123;this.handleCancel&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">          <span class="tag">&lt;<span class="name">img</span> <span class="attr">alt</span>=<span class="string">&quot;example&quot;</span> <span class="attr">style</span>=<span class="string">&#123;&#123;</span> <span class="attr">width:</span> &#x27;<span class="attr">100</span>%&#x27; &#125;&#125; <span class="attr">src</span>=<span class="string">&#123;previewImage&#125;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">Modal</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title class_">PicturesWall</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>（2）mymodel.js:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* eslint-disable */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; routerRedux &#125; <span class="keyword">from</span> <span class="string">&#x27;dva/router&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; message, <span class="title class_">Modal</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;antd&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;</span><br><span class="line">  getGoodsInfo,</span><br><span class="line">  getAllGoods,</span><br><span class="line">&#125; <span class="keyword">from</span> <span class="string">&#x27;../services/api&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; trim, getCookie &#125; <span class="keyword">from</span> <span class="string">&#x27;../utils/utils&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="attr">namespace</span>: <span class="string">&#x27;mymodel&#x27;</span>,</span><br><span class="line"></span><br><span class="line">  <span class="attr">state</span>: &#123;</span><br><span class="line">    <span class="attr">form</span>: &#123;&#125;,</span><br><span class="line">    <span class="attr">list</span>: [],</span><br><span class="line">    <span class="attr">listDetail</span>: [],</span><br><span class="line">    <span class="attr">goodsList</span>: [],</span><br><span class="line">    <span class="attr">goodsListDetail</span>: [],</span><br><span class="line">    <span class="attr">pagination</span>: &#123;</span><br><span class="line">      <span class="attr">pageSize</span>: <span class="number">10</span>,</span><br><span class="line">      <span class="attr">total</span>: <span class="number">0</span>,</span><br><span class="line">      <span class="attr">current</span>: <span class="number">1</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">imgList</span>: [], <span class="comment">//图片</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">subscriptions</span>: &#123;</span><br><span class="line">    <span class="title function_">setup</span>(<span class="params">&#123; dispatch, history &#125;</span>) &#123;</span><br><span class="line">      history.<span class="title function_">listen</span>(<span class="function"><span class="params">location</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (location.<span class="property">pathname</span> !== <span class="string">&#x27;/xx/xxx&#x27;</span>) <span class="keyword">return</span>;</span><br><span class="line">        <span class="keyword">if</span> (!location.<span class="property">state</span> || !location.<span class="property">state</span>.<span class="property">xxxId</span>) <span class="keyword">return</span>;</span><br><span class="line">        <span class="title function_">dispatch</span>(&#123;</span><br><span class="line">          <span class="attr">type</span>: <span class="string">&#x27;fetch&#x27;</span>,</span><br><span class="line">          <span class="attr">payload</span>: location.<span class="property">state</span>,</span><br><span class="line">        &#125;);</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="attr">effects</span>: &#123;</span><br><span class="line">    <span class="comment">// 接口。获取所有工厂店的列表 (步骤02)</span></span><br><span class="line">    *<span class="title function_">getAllInfo</span>(<span class="params">&#123; payload &#125;, &#123; select, call, put &#125;</span>) &#123;</span><br><span class="line">      <span class="keyword">yield</span> <span class="title function_">put</span>(&#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="string">&#x27;form&#x27;</span>,</span><br><span class="line">        payload,</span><br><span class="line">      &#125;);</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;params:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(payload));</span><br><span class="line"></span><br><span class="line">      <span class="keyword">let</span> params = &#123;&#125;;</span><br><span class="line">      params = payload;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">const</span> response = <span class="keyword">yield</span> <span class="title function_">call</span>(getGoodsInfo, params);</span><br><span class="line"></span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;smyhvae response:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(response));</span><br><span class="line">      <span class="keyword">if</span> (response.<span class="property">error</span>) <span class="keyword">return</span>;</span><br><span class="line">      <span class="keyword">yield</span> <span class="title function_">put</span>(&#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="string">&#x27;allInfo&#x27;</span>,</span><br><span class="line">        <span class="attr">payload</span>:</span><br><span class="line">          (response.<span class="property">data</span> &amp;&amp;</span><br><span class="line">            response.<span class="property">data</span>.<span class="title function_">map</span>(<span class="function"><span class="params">item</span> =&gt;</span> (&#123;</span><br><span class="line">              <span class="attr">xx1</span>: item.<span class="property">yy1</span>,</span><br><span class="line">              <span class="attr">xx2</span>: item.<span class="property">yy2</span>,</span><br><span class="line">            &#125;))) ||</span><br><span class="line">          [],</span><br><span class="line">      &#125;);</span><br><span class="line"></span><br><span class="line">      <span class="comment">// response 里包含了接口返回给前端的默认图片数据</span></span><br><span class="line">      <span class="keyword">if</span> (response &amp;&amp; response.<span class="property">data</span> &amp;&amp; response.<span class="property">data</span>[<span class="number">0</span>] &amp;&amp; response.<span class="property">data</span>[<span class="number">0</span>].<span class="property">my_jpg</span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> tempImgList = response.<span class="property">data</span>[<span class="number">0</span>].<span class="property">my_jpg</span>.<span class="title function_">split</span>(<span class="string">&#x27;,&#x27;</span>);</span><br><span class="line">        <span class="keyword">let</span> imgList = [];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (tempImgList.<span class="property">length</span> &gt; <span class="number">0</span>) &#123;</span><br><span class="line">          tempImgList.<span class="title function_">forEach</span>(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">            imgList.<span class="title function_">push</span>(&#123;</span><br><span class="line">              <span class="attr">uid</span>: item,</span><br><span class="line">              <span class="attr">name</span>: <span class="string">&#x27;xxx.png&#x27;</span>,</span><br><span class="line">              <span class="attr">status</span>: <span class="string">&#x27;done&#x27;</span>,</span><br><span class="line">              <span class="attr">thumbUrl</span>: item,</span><br><span class="line">            &#125;);</span><br><span class="line">          &#125;);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 通过  redux的方式 将 默认图片 传给 imgList</span></span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;smyhvae payload imgList:&#x27;</span> + <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(imgList));</span><br><span class="line">        <span class="keyword">yield</span> <span class="title function_">put</span>(&#123;</span><br><span class="line">          <span class="attr">type</span>: <span class="string">&#x27;setImgList&#x27;</span>,</span><br><span class="line">          <span class="attr">payload</span>: imgList,</span><br><span class="line">        &#125;);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    *<span class="title function_">setImgList</span>(<span class="params">&#123; payload &#125;, &#123; call, put &#125;</span>) &#123;</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;model setImgList&#x27;</span>);</span><br><span class="line">      <span class="keyword">yield</span> <span class="title function_">put</span>(&#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="string">&#x27;getImgList&#x27;</span>,</span><br><span class="line">        payload,</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="attr">reducers</span>: &#123;</span><br><span class="line">    <span class="title function_">allInfo</span>(<span class="params">state, action</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        ...state,</span><br><span class="line">        <span class="attr">list</span>: action.<span class="property">payload</span>,</span><br><span class="line">      &#125;;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">getImgList</span>(<span class="params">state, action</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        ...state,</span><br><span class="line">        <span class="attr">imgList</span>: action.<span class="property">payload</span>,</span><br><span class="line">      &#125;;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>上面的代码，可以规避 upload 组件的一些bug；而且可以在上传前，通过校验图片的尺寸、大小等，如果不满足条件，则弹出modal弹窗，阻止上传。</p>
<p>大功告成。本文感谢 ld 同学的支持。</p>
<p>更多内容，可以看本人的另外一篇文章：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://www.cnblogs.com/qianguyihao/p/13093592.html">AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</a></li>
</ul>
<h2 id="其他问题"><a href="#其他问题" class="headerlink" title="其他问题"></a>其他问题</h2><ul>
<li><a target="_blank" rel="noopener" href="https://github.com/ant-design/ant-design/issues/8779">beforeUpload返回false后，文件仍然为上传中的状态</a></li>
</ul>
<h2 id="最后一段"><a href="#最后一段" class="headerlink" title="最后一段"></a>最后一段</h2><p>有人说，前端开发，连卖菜的都会。可如果真的遇到技术难题，还是得找个靠谱的前端同学才行。这不，来看看前端码农日常：</p>
<p><img src="http://img.smyhvae.com/20190302_1339_2.png"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/">
            https://zwl5670.gitee.io/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">08-Ant Design的基本使用 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-AntD-%E7%9A%84-upload-%E7%BB%84%E4%BB%B6%E5%81%9A%E5%9B%BE%E7%89%87%E7%9A%84%E4%B8%8A%E4%BC%A0"><span class="toc-text">使用 AntD 的 upload 组件做图片的上传</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8A%E4%BC%A0%E5%90%8E%EF%BC%8C%E7%82%B9%E5%87%BB%E5%9B%BE%E7%89%87%E9%A2%84%E8%A7%88%EF%BC%8C%E6%B5%8F%E8%A7%88%E5%99%A8%E5%8D%A1%E6%AD%BB%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-text">上传后，点击图片预览，浏览器卡死的问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%B0%E9%9C%80%E6%B1%82%EF%BC%9A%E7%BC%96%E8%BE%91%E7%8E%B0%E6%9C%89%E9%A1%B5%E9%9D%A2"><span class="toc-text">新需求：编辑现有页面</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E9%97%AE%E9%A2%98"><span class="toc-text">其他问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%80%E5%90%8E%E4%B8%80%E6%AE%B5"><span class="toc-text">最后一段</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8-AntD-%E7%9A%84-upload-%E7%BB%84%E4%BB%B6%E5%81%9A%E5%9B%BE%E7%89%87%E7%9A%84%E4%B8%8A%E4%BC%A0"><span class="toc-text">使用 AntD 的 upload 组件做图片的上传</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8A%E4%BC%A0%E5%90%8E%EF%BC%8C%E7%82%B9%E5%87%BB%E5%9B%BE%E7%89%87%E9%A2%84%E8%A7%88%EF%BC%8C%E6%B5%8F%E8%A7%88%E5%99%A8%E5%8D%A1%E6%AD%BB%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-text">上传后，点击图片预览，浏览器卡死的问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%B0%E9%9C%80%E6%B1%82%EF%BC%9A%E7%BC%96%E8%BE%91%E7%8E%B0%E6%9C%89%E9%A1%B5%E9%9D%A2"><span class="toc-text">新需求：编辑现有页面</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E9%97%AE%E9%A2%98"><span class="toc-text">其他问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%80%E5%90%8E%E4%B8%80%E6%AE%B5"><span class="toc-text">最后一段</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
